﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZDSoft.UPMS.Model.BusinessPart;
using ZDSoft.UPMS.Service.BusinessPart;
using NHibernate.Criterion;
using ZDSoft.UPMS.Model.SystemPart;
using ZDSoft.UPMS.Core;

namespace ZDSoft.UPMS.Web.Controllers
{
    public class TermController : Controller
    {
        //
        // GET: /Term/

        public ActionResult Index()
        {
            return View();
        }
        public JsonResult GetPaged(int pageIndex, int pageSize, string strSearchProperty,
         string strSearchCondition, string strSortProperty, string strSortCondition)
        {
            IList<ICriterion> queryConditions = new List<ICriterion>();
            IList<Order> orderList = new List<Order>();
            if (strSearchProperty != "")
            {
                string[] searchProperties = strSearchProperty.Split(',');
                string[] searchConditions = strSearchCondition.Split(',');
                queryConditions = new List<ICriterion>();
                for (int i = 0; i < searchProperties.Length; i++)
                {
                    queryConditions.Add(Expression.Eq(searchProperties[i], searchConditions[i]));
                }
            }
            if (strSortProperty != "")
            {
                string[] sortProperties = strSortProperty.Split(',');
                string[] sortConditions = strSortCondition.Split(',');
                orderList = new List<Order>();
                for (int i = 0; i < sortProperties.Length; i++)
                {
                    if (sortConditions[i] == "asc")
                    {
                        orderList.Add(new Order(sortProperties[i], true));
                    }
                    else
                    {
                        orderList.Add(new Order(sortProperties[i], false));
                    }
                }
            }

            int count;
            int pageNum;

            IList<Term> list = new List<Term>();
            IList<Term>  Terms = ZDSoft.UPMS.Core.Container.Instance.Resolve<ITermService>().GetPaged(queryConditions, orderList, pageIndex, pageSize, out count);

            int tmpcountI = count / pageSize;
            float temCountF = float.Parse(count.ToString()) / float.Parse(pageSize.ToString());
            if (temCountF > float.Parse(tmpcountI.ToString()))
            {
                pageNum = tmpcountI + 1;
            }
            else
            {
                pageNum = tmpcountI;
            }

            foreach (Term  Term  in Terms)
            {
                JsonHelper.ToJaonResult<Term>(Term);
                list.Add(Term);

            }
            list.Add(new  Term() { ID = pageNum });
            JsonResult result = this.Json(list, JsonRequestBehavior.AllowGet);
            return result;
        }

        //
        // GET: /Term/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Term/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Term/Create

        [HttpPost]
        public void Create(Term term)
        {
            ZDSoft.UPMS.Core.Container.Instance.Resolve<ITermService>().Create(term);
        }
        
        //
        // GET: /Term/Edit/5
 
        public ActionResult Update(int id)
        {
            Term term = ZDSoft.UPMS.Core.Container.Instance.Resolve<ITermService>().FindByID(id);
            return View(term);
        }

        //
        // POST: /Term/Edit/5

        [HttpPost]
        public void Update(Term terms)
        {
            Term term = ZDSoft.UPMS.Core.Container.Instance.Resolve<ITermService>().FindByID(terms.ID);
            ModelUtilityHelper.Transfer(terms, term);
            ZDSoft.UPMS.Core.Container.Instance.Resolve<ITermService>().Update(term);
        }

        //
        // GET: /Term/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Term/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        public string  GetById(string id)
        {
            Term entity = ZDSoft.UPMS.Core.Container.Instance.Resolve<ITermService>().FindByID(int.Parse(id));
            IList<KeyValuePair<string, InvokeMethod>> list = new List<KeyValuePair<string, InvokeMethod>>();
            string str = entity.ToJsonString<Term>(list);
            return str;
        }
    }
}
